package model.server;

import java.net.Socket;
import model.cache.CacheController;
import model.data.Result;
import model.search.Keywords;

public class ClientHandler extends Thread {

	private ConnectionHandler client;

	private CacheController cacheController;

	public ClientHandler(Socket client, CacheController cacheController) {
		this.client = new ConnectionHandler(client);
		this.cacheController = cacheController;
	}

	public void run() {
		String requestString = client.readAll();
		if (requestString == null || requestString == "")
			return;
		Request request = new Request(requestString);
		Keywords keywords = new Keywords(request.searchString());
		Result result = cacheController.getValue(request.category(), request
				.province(), request.county(), keywords);
		Response response = new Response(result);
		client.sendAll(response.toString());
		client.close();
	}
}
